﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using WS.EKA.Utility;
using WS.EKA.SyncData.EventSourceMSMQ;

namespace WS.EKA.SyncData
{
    public class SyncData
    {
        static readonly Deppon.EventService service;

        static SyncData()
        {
            service = new Deppon.EventService();
            service.Url = ConfigurationManager.AppSettings["EventService"];
        }

        public static void ExecuteByWebService(string target, string data)
        {
            Execute(target, data, x =>
            {
                service.SaveEventByString(x);
            });
        }
        public static void ExecuteByMSMQ(string target, string data)
        {
            Execute(target, data, x =>
            {
                using (var proxy = new EventSourceMSMQClient())
                {
                    proxy.SaveEventByString(x);
                }
            });
        }

        private static void Execute(string target, string data, Action<string> action)
        {
            Console.WriteLine(string.Format("Service type is:{0}", target));
            Console.WriteLine(string.Format("Data is :{0}", data));
            Logger.Log(string.Format("Service type is:{0}", target));
            Logger.Log(string.Format("Data is :{0}", data));

            try
            {
                data = data.Replace("\\`", "\"");
                action(data);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Logger.Error("SyncData Error. ", ex);
            }
        }

    }
}
